<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\User;
use app\models\Address;
use app\models\ShoppingCart;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class MemberController extends BaseController
{
    /**
     * @param string $type
     * @return array
     */
    public function actionMemberList(){
        $page = Yii::$app->request->post('page', 0);
        $nickname = Yii::$app->request->post('nickname', '');
        $name = Yii::$app->request->post('name', '');
        $phone = Yii::$app->request->post('phone', '');
        $query = User::find()->where(['status' => 1]);
        if ($nickname) {
          $query->andWhere(['like', 'nickname', $nickname]);
        }
        if ($name) {
          $query->andWhere(['like', 'name', $name]);
        }
        if ($phone) {
          $query->andWhere(['like', 'phone', $phone]);
        }
        $list = $query->orderBy("created_at desc")->limit(User::PAGE_SIZE)->offset(User::PAGE_SIZE*$page)->all();
        $user_data = [];
        if ($list) {
          foreach ($list as $v) {
              $user_data[] = [
                  'id' => $v->id,
                  'nickname' => $v->nickname,
                  'phone' => $v->phone,
                  'name' => $v->name,
                  'invitation_name' => $v->invitation_id ? $v->invitation->name : '/',
                  'created_time' => date("Y-m-d H:i", $v->created_at),
              ];
          }
        }
        return Re::data([
            'data' => $user_data,
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => User::PAGE_SIZE
        ]);
    }

    public function actionCartList(){
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', '');
        $good_name = Yii::$app->request->post('good_name', '');
        $query = ShoppingCart::find()
          ->leftJoin('user', 'shopping_cart.user_id=user.id')
          ->leftJoin('good', 'shopping_cart.good_id=good.id')
          ->where(['shopping_cart.status' => 1]);
        if ($name) {
          $query->andWhere(['like', 'name', $name]);
        }
        if ($good_name) {
            $query->andWhere(['like', 'good.name', $good_name]);
        }
        $list = $query->orderBy("created_at desc")->limit(ShoppingCart::PAGE_SIZE)->offset(ShoppingCart::PAGE_SIZE*$page)->all();
        $cart_data = [];
        if ($list) {
            foreach ($list as $v) {
                $cart_data[] = [
                    'id' => $v->id,
                    'name' => $v->user->name,
                    'good_name' => $v->good->name,
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $cart_data,
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => ShoppingCart::PAGE_SIZE
        ]);
    }

    public function actionAddressList(){
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', '');
        $phone = Yii::$app->request->post('phone', '');
        $query = Address::find()
          ->leftJoin('user', 'address.user_id=user.id')
          ->where(['address.status' => 1]);
        if ($name) {
            $query->andWhere(['like', 'name', $name]);
        }
        if ($phone) {
            $query->andWhere(['like', 'address.phone', $phone]);
        }
        $list = $query->orderBy("created_at desc")->limit(Address::PAGE_SIZE)->offset(Address::PAGE_SIZE*$page)->all();
        $address_data = [];
        if ($list) {
            foreach ($list as $v) {
                $address_data[] = [
                    'id' => $v->id,
                    'user_id' => $v->user_id,
                    'name' => $v->name,
                    'phone' => $v->phone,
                    'area_name' => $v->area->parentArea->parentArea->name.'/'.$v->area->parentArea->name.'/'.$v->area->name,
                    'p_id' => (int)$v->area->parentArea->parentArea->id,
                    'c_id' => (int)$v->area->parentArea->id,
                    'area_id' => (int)$v->area_id,
                    'address' => $v->address,
                    'default' => $v->default,
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $address_data,
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => Address::PAGE_SIZE
        ]);
    }

    public function actionEditAddress(){
        $id = Yii::$app->request->post('id', 0);
        $user_id = Yii::$app->request->post('user_id', '');
        $name = Yii::$app->request->post('name', '');
        $phone = Yii::$app->request->post('phone', '');
        $area_id = Yii::$app->request->post('area_id', 0);
        $address = Yii::$app->request->post('address', '');
        $default = Yii::$app->request->post('default', 0);
        $address_info = Address::findOne($id);
        if (empty($address_info)) {
          return Re::data(null, '地址信息读取失败', Re::ERROR);
        }
        if ($default) {
          Address::updateAll(['default' => 0], ['user_id' => $user_id]);
        }
        $address_info->user_id = $user_id;
        $address_info->name = $name;
        $address_info->phone = $phone;
        $address_info->area_id = $area_id;
        $address_info->address = $address;
        $address_info->default = $default;
        //$address_info->created_at = time();
        if (!$address_info->save()) {
            var_dump($address_info->getErrors());die;
          return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }
}
